package com.fourforfo.fourmall.order.config;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.AbstractJackson2MessageConverter;
import org.springframework.amqp.support.converter.ClassMapper;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.time.Duration;
import java.util.stream.Collectors;


/*
    因为有如下依赖问题，所以将MessageConverter和RabbitTemplate分为两个类
    ┌─────┐
    |  rabbitTemplate defined in class path resource [org/springframework/boot/autoconfigure/amqp/RabbitAutoConfiguration$RabbitTemplateConfiguration.class]
    ↑     ↓
    |  myRabbitConfig (field org.springframework.amqp.rabbit.core.RabbitTemplate com.fourforfo.fourmall.order.config.MyRabbitConfig.rabbitTemplate)
    └─────┘
 */
@Configuration
class MessageConverterConfig{
    /**
     * 设置编码格式为Json
     * @return
     */
    @Bean
    public MessageConverter messageConverter(){
        return new Jackson2JsonMessageConverter();
    }

}

/**
 * @program: xxl-sso-core
 * @description: 自定义RabbitMQ的配置，设置编码方式等
 * @author: fourforfo
 * @email: fourforfo@gmail.com
 * @create: 2022-08-29 20:14
 **/
@EnableRabbit
@Configuration
public class MyRabbitConfig {

//    /**
//     * 设置编码格式为Json
//     * @return
//     */
//    @Bean
//    public MessageConverter messageConverter(){
//        return new Jackson2JsonMessageConverter();
//    }

    @Autowired
    RabbitTemplate rabbitTemplate;

    //customize RabbitTemplate
    @PostConstruct
    public void initRabbitTemplate(){
        RabbitTemplate.ConfirmCallback confirmCallback = new RabbitTemplate.ConfirmCallback() {

            /**
             *
             * @param correlationData 当前消息的唯一关联数据（这是消息的唯一id）
             * @param ack 消息是否成功收到
             * @param cause 失败原因
             */
            @Override
            public void confirm(CorrelationData correlationData, boolean ack, String cause) {

                System.out.println("confirm...correlationData=[" + correlationData + "]==>ack=[" + ack + "]==>cause=[" + cause + "]");
            }
        };
        rabbitTemplate.setConfirmCallback(confirmCallback);


        RabbitTemplate.ReturnCallback returnCallback = new RabbitTemplate.ReturnCallback() {

            /**
             * 只有消息没有投递到指定的队列，才触发这个失败回调
             * @param message 投递失败的时的详细消息
             * @param replyCode 回复的状态码
             * @param replyText 恢复的文本内容
             * @param exchange 当时该消息是发给哪个交换机
             * @param routingKey 当时该消息发送时的路由健
             */
            @Override
            public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {
//                System.out.println("returnCallback...message=["+message+"]");
                System.out.println("returnCallback...replyText=["+replyText+"]==>exchange=["+exchange+"]+routingKey=["+routingKey+"]");
            }
        };

        rabbitTemplate.setReturnCallback(returnCallback);
    }



}
